package com.financial.service.heemoney.service.impl;

import com.financial.service.heemoney.annotation.ValidateParam;
import com.financial.service.heemoney.constants.FinancialProregisterHeemoneylogEnumConstants;
import com.financial.service.heemoney.constants.FinancialRegisterHeemoneylogEnumConstants;
import com.financial.service.heemoney.convertor.HeemoneyRegisterConvert;
import com.financial.service.heemoney.dao.FinancialProregisterHeemoneylogDao;
import com.financial.service.heemoney.dao.FinancialRegisterHeemoneylogDao;
import com.financial.service.heemoney.domain.FinancialProregisterHeemoneylogDO;
import com.financial.service.heemoney.domain.FinancialRegisterHeemoneylogDO;
import com.financial.service.heemoney.dto.HeemoneyConfigDTO;
import com.financial.service.heemoney.dto.businsess.HeemoneyRegisterBusinessDTO;
import com.financial.service.heemoney.dto.commmon.HeemoneyRegisterCommonDTO;
import com.financial.service.heemoney.dto.inparameter.HeemoneyRegisterInParamDTO;
import com.financial.service.heemoney.dto.param.HeemoneyRegisterParamDTO;
import com.financial.service.heemoney.service.FinancialRegisterHeemoneylogService;
import com.financial.service.heemoney.utils.JsonUtils;
import com.financial.service.heemoney.utils.OkHttpUtils;
import com.financial.service.heemoney.utils.ResultUtils;
import com.financial.service.heemoney.utils.apiresult.ApiResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class FinancialRegisterHeemoneylogServiceImpl implements FinancialRegisterHeemoneylogService {
    @Autowired
    private FinancialRegisterHeemoneylogDao financialRegisterHeemoneylogDao;
    @Autowired
    private FinancialProregisterHeemoneylogDao financialProregisterHeemoneylogDao;
    @Autowired
    private OkHttpUtils okHttpUtils;
    public static final Logger log = LoggerFactory.getLogger(FinancialPayHeemoneyServiceImpl.class);


    @Override
    @ValidateParam("com.financial.service.heemoney.dto.inparameter.HeemoneyRegisterInParamDTO")
    public ApiResult register(HeemoneyRegisterInParamDTO heemoneyRegisterInParamDTO) {
        log.info("调用金融中心汇收银服务层商户简要注册接口前参数打印：{}", JsonUtils.beanToJson(heemoneyRegisterInParamDTO));
        //保存商户注册信息记录
        FinancialProregisterHeemoneylogDO registerLogDO = this.saveRegister(heemoneyRegisterInParamDTO);
        //数据转实体校验参数非空
        HeemoneyRegisterCommonDTO heemoneyRegisterCommonDTO = HeemoneyRegisterConvert.MAPPER.configDTOConvertCommonDTO(new HeemoneyConfigDTO(), heemoneyRegisterInParamDTO);
        HeemoneyRegisterBusinessDTO heemoneyRegisterBusinessDTO = HeemoneyRegisterConvert.MAPPER.configDTOConvertBusinessDTO(new HeemoneyConfigDTO(), heemoneyRegisterInParamDTO);
        HeemoneyRegisterParamDTO heemoneyRegisterParamDTO = new HeemoneyRegisterParamDTO(heemoneyRegisterCommonDTO, heemoneyRegisterBusinessDTO);
        log.info("http请求汇收银服务层商户简要注册接口入参：{}", heemoneyRegisterParamDTO.getJsonParam());
        try {
            String resultStr = okHttpUtils.postJsonParams(new HeemoneyConfigDTO().getHeemoneyRegister().getRequest_url(), heemoneyRegisterParamDTO.getJsonParam());
            log.info("http请求汇收银服务层商户简要注册接口回执:{}", resultStr);
            Map<String, Object> strMap = JsonUtils.jsonToMap(resultStr);
            //账户类appid
            strMap.put("app_id_account", new HeemoneyConfigDTO().getHeemoneyAccountType().getApp_id());
            //支付类
            strMap.put("app_id", new HeemoneyConfigDTO().getHeemoneyPayType().getApp_id());
            //付款类
            strMap.put("app_id_settle", new HeemoneyConfigDTO().getHeemoneyPaymentType().getApp_id());

            String convertMap = JsonUtils.beanToJson(strMap);

            //修改注册信息
            this.updateRegisterInfo(resultStr, registerLogDO);
            //返回调用信息
            return ResultUtils.resultStr(convertMap);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("http请求汇收银服务层商户简要注册接口失败：{}", e.getMessage());
        }
        return ApiResult.failure();

    }

    private FinancialProregisterHeemoneylogDO saveRegister(HeemoneyRegisterInParamDTO heemoneyRegisterInParamDTO) {
        FinancialProregisterHeemoneylogDO registerHeemoneylogDO = new FinancialProregisterHeemoneylogDO();

        try {
            BeanUtils.copyProperties(heemoneyRegisterInParamDTO, registerHeemoneylogDO);
            registerHeemoneylogDO.setIsDelete(FinancialProregisterHeemoneylogEnumConstants.ENUM_ISDELETE_UNDEL);
            registerHeemoneylogDO.setGmtCreate(new Date());
            registerHeemoneylogDO.setGmtModified(new Date());
            financialProregisterHeemoneylogDao.save(registerHeemoneylogDO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("汇收银商户账号：{}注册日志保存失败!", heemoneyRegisterInParamDTO.getMch_account());
        }
        return registerHeemoneylogDO;
    }

    private void updateRegisterInfo(String resultStr, FinancialProregisterHeemoneylogDO registerLogDO) {
        Object temp = "";
        Map<String, Object> strMap = JsonUtils.jsonToMap(resultStr);
        temp = strMap.get("merch_sub_uid");
        String merch_sub_uid = temp != null ? temp.toString() : null;
        String result_code = strMap.get("result_code").toString();
        try {
            registerLogDO.setGmtModified(new Date());
            registerLogDO.setIsDelete(FinancialRegisterHeemoneylogEnumConstants.ENUM_ISDELETE_UNDEL);
            registerLogDO.setMchUid(merch_sub_uid);
            if (!result_code.equals("SUCCESS")) {
                registerLogDO.setState(FinancialRegisterHeemoneylogEnumConstants.ENUM_STATE_FAIL);
            } else {
                registerLogDO.setState(FinancialRegisterHeemoneylogEnumConstants.ENUM_STATE_SUC);
            }
            registerLogDO.setReturnMessage(resultStr);
            financialProregisterHeemoneylogDao.update(registerLogDO);
        } catch (Exception e) {
            log.error("修改汇收银商户简要注册信息失败！:{}", strMap.get("error_msg"));
            e.printStackTrace();
        }

    }

/*
	private FinancialRegisterHeemoneylogDO saveRequest(HeemoneyRegisterInParamDTO heemoneyRegisterInParamDTO) {
        FinancialRegisterHeemoneylogDO registerLogDO = new FinancialRegisterHeemoneylogDO();
		try{
			registerLogDO.setGmtCreate(new Date());
			registerLogDO.setIsDelete(FinancialRegisterHeemoneylogEnumConstants.ENUM_ISDELETE_UNDEL);
			registerLogDO.setPlatformNo(heemoneyRegisterInParamDTO.getPlatform_no());
			registerLogDO.setRequestMessage(JsonUtils.beanToJson(heemoneyRegisterInParamDTO));
			registerLogDO.setCompany(heemoneyRegisterInParamDTO.getCompany());
			registerLogDO.setState(FinancialRegisterHeemoneylogEnumConstants.ENUM_STATE_WAIT);
			financialRegisterHeemoneylogDao.save(registerLogDO);
		}catch (Exception e2){
			log.error("请求汇收银商户简要注册请求参数日志保存失败！商户账户为:{}",heemoneyRegisterInParamDTO.getMch_account());
			e2.printStackTrace();
		}
        return registerLogDO;
	}
*/

    @Override
    public FinancialRegisterHeemoneylogDO get(Integer id) {
        return financialRegisterHeemoneylogDao.get(id);
    }

    @Override
    public List<FinancialRegisterHeemoneylogDO> list(Map<String, Object> map) {
        return financialRegisterHeemoneylogDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return financialRegisterHeemoneylogDao.count(map);
    }

    @Override
    public int save(FinancialRegisterHeemoneylogDO financialRegisterHeemoneylog) {
        return financialRegisterHeemoneylogDao.save(financialRegisterHeemoneylog);
    }

    @Override
    public int update(FinancialRegisterHeemoneylogDO financialRegisterHeemoneylog) {
        return financialRegisterHeemoneylogDao.update(financialRegisterHeemoneylog);
    }

    @Override
    public int remove(Integer id) {
        return financialRegisterHeemoneylogDao.remove(id);
    }

    @Override
    public int batchRemove(Integer[] ids) {
        return financialRegisterHeemoneylogDao.batchRemove(ids);
    }


}
